﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.Domain.Interfaces;
using WebNext.Domain.Entities;
using WebNext.UI.Models;
using WebNext.UI.Models.Tags;
using WebNext.UI.Utils;
using WebNext.Domain;
using WebNext.UI.Properties;

namespace WebNext.UI.Controllers
{
	public class TagsController : BaseController
	{
		private ITagsRepository tagsRepository;

		public TagsController(ITagsRepository tagsRepository)
		{
			this.tagsRepository = tagsRepository;
			this.tagsRepository.CultureId = this.CultureId;
		}

		public ActionResult List(MainViewType viewType, string selectedTag)
		{
			IEnumerable<Tag> tags = this.tagsRepository.GetAll();
			TagsViewModel tagsModel = new TagsViewModel(tags, this.IsLogged);
			tagsModel.FilterConfig.Tag = selectedTag;
			tagsModel.FilterConfig.FilterMode = FilterMode.Tag;
			tagsModel.SetViewType(viewType);
			return this.View("List", tagsModel);
		}

		[Authorize]
		public ActionResult Edit()
		{
			IEnumerable<Tag> tags = this.tagsRepository.GetAll();
			TagsViewModel tagsModel = new TagsViewModel(tags, this.IsLogged);
			if (this.TempData[Constants.Temp] is TagViewModel)
			{
				TagViewModel editedTagModel = this.TempData[Constants.Temp] as TagViewModel;
				tagsModel.Merge(editedTagModel);
			}
			return this.View(tagsModel);
		}

		[HttpPost]
		[Authorize]
		public ActionResult Edit(TagViewModel tagModel)
		{
			if (this.ValidateTag(tagModel))
			{
				if (tagModel.Id == 0)
				{
					this.AddNewTag(tagModel);
				}
				else
				{
					this.UpdateTag(tagModel);
				}
			}
			else
			{
				this.TempData[Constants.Temp] = tagModel;
			}
			return this.RedirectToAction("Edit");
		}

		private void AddNewTag(TagViewModel tagModel)
		{
			Tag tag = new Tag(tagModel.Name, tagModel.Code, this.CultureId);
		   this.tagsRepository.Save(tag);
			this.TempData[Constants.Message] = string.Format(Properties.Resources.TagSaved, tagModel.Name);
			tagModel.Name = null;
			tagModel.Code = null;
		}

		private void UpdateTag(TagViewModel tagModel)
		{
			Tag tag = this.tagsRepository.GetById(tagModel.Id);
		   if (tag == null)
		   {
				this.TempData[Constants.Message] = string.Format(Properties.Resources.TagOfIdNotFound, tagModel.Id);
		   }
		   else
		   {
				tag.Name = tagModel.Name;
				tag.Code = tagModel.Code;
				this.TempData[Constants.Message] = string.Format(Properties.Resources.TagSaved, tagModel.Name);
		   }
		}

		private bool ValidateTag(TagViewModel tagModel)
		{
			ValidationResult validationResult = tagModel.Validate();
			if (!validationResult.IsValid)
			{
				this.TempData[Constants.Message] = validationResult.GetErrorsMessage();
				return false;
			}

			if (this.tagsRepository.CheckIfTagNameIsUsed(tagModel.Id, tagModel.Name))
			{
				this.TempData[Constants.Message] = string.Format(Properties.Resources.TagOfNameAlreadyExists, tagModel.Name);
				return false;
			}

			if (this.tagsRepository.CheckIfTagCodeIsUsed(tagModel.Id, tagModel.Code))
			{
				this.TempData[Constants.Message] = string.Format(Properties.Resources.TagOfCodeAlreadyExists, tagModel.Code);
				return false;
			}

			return true;
		}

		[HttpPost]
		[Authorize]
		public ActionResult Delete(int tagId)
		{
			Tag tag = this.tagsRepository.GetById(tagId);
			if (tag == null)
			{
				this.TempData[Constants.Message] = string.Format(Properties.Resources.TagOfIdNotFound, tagId);
			}
			else
			{
				this.tagsRepository.Delete(tag);
				this.TempData[Constants.Message] = string.Format(Properties.Resources.TagDeleted, tag.Name);
			}
			return this.RedirectToAction("Edit");
		}
	}
}
